<?php


class Role_model extends Base_model {

	/**
	 * 表名
	 * 
	 * @param string 
	 */
    public $_table_name = "admin_role";


    /**
     * status状态：1 正常    2 禁用(禁用时, 属于此角色的所有用户将无法登录)
     */
    const STATUS_SUCCESS = 1;
    const STATUS_FAILED = 2;


     /**
     * 状态数组
     * @var array 
     */
	public static $status_arr = array(self::STATUS_SUCCESS => '正常', self::STATUS_FAILED => '禁用');


    /**
     * 状态
     * @var string 
     */
    public $status;


    /**
     * 角色名称
     * @var string 
     */
    public $role_name;


	/**
     * 权限
     * @var string 
     */
    public $permissions;



    /**
     * 插入数据前验证
     * 
     * @return boolean 
     */
    protected function prepare_insert() {

		if(empty($this->role_name) OR empty($this->permissions))
		{
            return false;
        }

        if(empty($this->status)) {
            $this->status = self::STATUS_SUCCESS;
        }

        return true;
    }

    
    /**
     * 搜索
     * 
     * @return boolean true | false
     */
    public function search() {
        $sql = "SELECT ";

        if (!empty($this->_is_search_counter))
		{
            $sql .= " COUNT(adro.id) AS rows_total ";
        }
		else
		{
            $sql .= " adro.*, COUNT(admn.id) AS admin_counts ";
        }

        $sql .= " FROM {$this->_table_name} AS adro ";

		if(empty($this->_is_search_counter))
		{
			$sql .= " LEFT JOIN {$this->db->dbprefix}admin AS admn ON admn.role_id = adro.id ";
		}

        $statement = "";
        $where = array();

        if (!empty($this->id)) {
            $statement .= " AND adro.id = ? ";
            $where[] = $this->id;
        }

        if (!empty($this->status)) {
            $statement .= " AND adro.status = ? ";
            $where[] = $this->status;
        }

		//LIKE搜索转义
        if (!empty($this->role_name)) {
            $statement .= " AND adro.role_name LIKE '%" . $this->db->escape_like_str($this->role_name). "%' ";
        }

		//处理条件
        if($statement) {
            $sql .= " WHERE " . substr($statement, 4);
        }


        if (!empty($this->_is_search_counter))
		{
            $sql .= " LIMIT 1";
        }
		else
		{
			$sql .= " GROUP BY adro.id ";

            if (!empty($this->_order_by)) {
                $sql .= " ORDER BY {$this->_order_by} ";
            }

			//是否分页
            if ($this->is_paging()) {

				$page_from = ($this->_page_index - 1) * $this->_page_count;

                $sql .= " LIMIT {$page_from}, {$this->_page_count}";
            }
        }

        $result = $this->db->query($sql, $where);

        if(empty($this->_is_search_counter))
		{
			$this->_search_results = $result->result();
        }
		else
		{
            if ($result->num_rows() > 0) {
                $row = $result->row();

                if (!empty($row->rows_total)) {
                    $this->_search_num_rows = $row->rows_total;
                }
            }
        }

        $result->free_result();

        return true;
    }


	//获取所有的角色列表
    public function get_all_roles()
	{
        $sql = "SELECT  adro.id, adro.role_name FROM {$this->_table_name} AS adro";
        $result = $this->db->query($sql);
        return $result->result();
    }


}